﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using TRPOP.CRM.Component.Common;
using TRPOP.CRM.Component.Model;
using TRPOP.CRM.Component.Business;
using System.Text;

namespace TRPOP.CRM.Web
{
    public partial class sidebar : BaseAdmin
    {
        string m_strVarJS = "var tree = new WebFXTree(\"{0}\", \"\", \"\", \"\");" + System.Environment.NewLine;
        string m_strItem = "tree.add(new WebFXTreeItem(\"{0}\", \"{1}\", \"iframeList\", 10, \"\", _BaseFileImgURL));" + System.Environment.NewLine;
        string m_strVarJSEnd = "document.write(tree);" + System.Environment.NewLine;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CheckPermissionURL();
                LoadBase();
            }
        }

        /// <summary>
        /// 根据权限加载不同的菜单
        /// </summary>
        void CheckPermissionURL()
        {
            string strTemplateHtml = lbTemplate.Text;

            StringBuilder strBuild = new StringBuilder();

            //  系统设置
            {
                Dictionary<string, string> oDic = new Dictionary<string, string>();
                oDic.Add("sys_", "基础数据设置");
                oDic.Add("employee_", "员工信息管理");
                oDic.Add("remindcategory_", "工作计划类型设置");

                if (CheckIfContainKey(oDic))
                {
                    AppendHtml(strTemplateHtml, "系统设置", strBuild, oDic);
                }
            }

            //  客户管理
            {

                Dictionary<string, string> oDic = new Dictionary<string, string>();
                oDic.Add("company_", "客户信息管理");
                oDic.Add("company2_", "客户分配管理");

                if (CheckIfContainKey(oDic))
                {
                    AppendHtml(strTemplateHtml, "客户管理", strBuild, oDic);
                }
            }

            lbTemplate.Text = strBuild.ToString();
            lbTemplate.Visible = true;
        }

        bool CheckIfContainKey(Dictionary<string, string> oDic)
        {
            foreach (var itemVar in oDic)
            {
                if (base.m_oCurrentPermissionInfos.Exists(oInfo => oInfo.nvcPermissionCode.ToLower().Contains(itemVar.Key)))
                {
                    return true;
                }
            }

            return false;
        }

        void AppendHtml(string strTemplateHtml, string strName, StringBuilder strBuild, Dictionary<string, string> oDic)
        {
            string strTemp = strTemplateHtml.Replace("$$title$$", strName);

            string strJSTemp = string.Empty;

            foreach (var itemVar in oDic)
            {
                if (base.m_oCurrentPermissionInfos.Exists(oinfo => oinfo.nvcPermissionCode.ToLower().Contains(itemVar.Key)))
                {
                    strJSTemp += string.Format(m_strVarJS, itemVar.Value);

                    foreach (PermissionInfo oPermissionInfo in base.m_oCurrentPermissionInfos.FindAll(oInfo => oInfo.nvcPermissionCode.ToLower().Contains(itemVar.Key)))
                    {
                        strJSTemp += string.Format(m_strItem, oPermissionInfo.nvcName, oPermissionInfo.nvcPageURL); ;
                    }

                    strJSTemp += m_strVarJSEnd;
                }
            }

            strBuild.Append(strTemp.Replace("$$JS$$", strJSTemp));
        }
        
        /// <summary>
        /// 加载基本的信息
        /// </summary>
        void LoadBase()
        {
            string strItem = ltallRemind.Text;

            StringBuilder strBuilder = new StringBuilder();

            foreach (RemindCategoryInfo oRemindCategoryInfo in BaseManage.GetAllRemindCategoryInfo())
            {
                strBuilder.Append(string.Format(strItem, oRemindCategoryInfo.nvcRemindCategoryName, oRemindCategoryInfo.iRemindCategoryID));
            }

            ltallRemind.Text = strBuilder.ToString();
        }
    }
}